﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Plugins.ContentTree;
using MethodWorx.CMS.Providers;
using MethodWorx.CMS.Services;
using System.Web.Mvc;
using MethodWorx.CMS.Plugins.ActionResults;
using MethodWorx.CMS.Web;

namespace MethodWorx.CMS.Plugins.MasterPageTemplates
{
    [CmsPlugin(Name="Master Page Templates", Description="Manages Master Page Templates", Id="{15B6B8E6-9DD3-447B-8497-B3D86D08C7A2}")]
    public class MasterPageTemplatesExtension : Plugin, IContentTreePlugin
    {
        public IEnumerable<TreeNode> GetTreeNodes(string objectType, Guid? objectId)
        {
            if (string.IsNullOrEmpty(objectType))
            {
                return new TreeNode[] { new TreeNode()
                {
                    Closed = true,
                    Icon = "~/images/icons/folder.png",
                    ObjectId = null,
                    ObjectType = "MasterPageTemplates",
                    Title = "Master Page Templates",
                    MenuItems = GetRootMenuItems(true),
                    ToolbarButtons = GetRootToolbarButtons(true)
                }, new TreeNode()
                {
                    Closed = true,
                    Icon = "~/images/icons/folder.png",
                    ObjectId = null,
                    ObjectType = "PageTemplates",
                    Title = "Page Templates",
                    MenuItems = GetRootMenuItems(false),
                    ToolbarButtons = GetRootToolbarButtons(false)
                }, new TreeNode()
                {
                    Closed = true,
                    Icon = "~/images/icons/folder.png",
                    ObjectId = null,
                    ObjectType = "ContentTemplates",
                    Title = "Content Templates",
                    ToolbarButtons = GetRootContentMenuItems()
                }};
            }
            else if (objectType == "MasterPageTemplates")
            {
                SchemaService schema = new SchemaService(this.CmsDataProvider);

                return schema.GetAbstractPageTemplates().
                  Select(pt => new
                  TreeNode
                  {
                      ObjectType = "MasterPageTemplate",
                      ObjectId = pt.Id,
                      Title = pt.Name,
                      Icon = "~/images/icons/layout.png",
                      Closed = true,
                      DefaultAction = "EditTemplate",
                      MenuItems = GetTemplateMenuItems(),
                      ToolbarButtons = GetTemplateToolbarButtons()
                  });
            }
            else if (objectType == "PageTemplates")
            {
                SchemaService schema = new SchemaService(this.CmsDataProvider);

                return schema.GetAllPageTemplates().Where(t => t.Abstract == false).
                  Select(pt => new
                  TreeNode
                  {
                      ObjectType = "PageTemplate",
                      ObjectId = pt.Id,
                      Title = pt.Name,
                      Icon = "~/images/icons/layout.png",
                      Closed = true,
                      MenuItems = GetTemplateMenuItems(),
                      ToolbarButtons = GetTemplateToolbarButtons(),
                      DefaultAction = "EditTemplate"
                  });
            }
            else if (objectType == "ContentTemplates")
            {
                SchemaService schema = new SchemaService(this.CmsDataProvider);

                return schema.GetAllContentTemplates().
                  Select(pt => new
                  TreeNode
                  {
                      ObjectType = "ContentTemplate",
                      ObjectId = pt.Id,
                      Title = pt.Name,
                      Icon = "~/images/icons/layout.png",
                      Closed = true,
                      MenuItems = GetTemplateMenuItems(),
                      ToolbarButtons = GetTemplateToolbarButtons(),
                      DefaultAction = "EditTemplate"
                  });
            }
            else
                return null;
        }

        private ToolbarButton[] GetRootContentMenuItems()
        {
            return new ToolbarButton[]
                       {
                           new ToolbarButton()
                                      {
                                        Id = "CreateContentTemplate",
                                        Icon = "~/images/icons/page_add.png",
                                        Title = "Create Content Template"
                                      }
                       };
        }

        private TreeNodeMenuItem[] GetTemplateMenuItems()
        {
            return new TreeNodeMenuItem[]
            {
                new TreeNodeMenuItem() 
                {
                    Disabled = false,
                    Id = "EditTemplate",
                    Title = "Edit"
                },
                new TreeNodeMenuItem()
                {
                    Disabled = false,
                    Id="DeleteTemplate",
                    Title="Delete",
                    Confirm="Are you sure you want to delete this master page template?"
                },
                new TreeNodeMenuItem()
                {
                    Disabled = false,
                    Id="Security",
                    Title="Security"
                }
            };
        }

        private ToolbarButton[] GetRootToolbarButtons(bool master)
        {
            if (master)
            {
                return new ToolbarButton[]
                           {
                               new ToolbarButton()
                                   {
                                       Title = "Create Master Page Template",
                                       Id = "CreateMasterPageTemplate",
                                       Icon = "~/Images/Icons/layout_add.png"
                                   },
                           };
            }
            else
            {
                return new ToolbarButton[]
                           {
                               new ToolbarButton()
                                   {
                                       Title = "Create Page Template",
                                       Id = "CreatePageTemplate",
                                       Icon = "~/Images/Icons/layout_add.png"
                                   },
                           };
            }
        }

        private ToolbarButton[] GetTemplateToolbarButtons()
        {
            return new ToolbarButton[]
            {
                new ToolbarButton()
                {
                    Title = "Edit Template",
                    Id = "EditTemplate",
                    Icon = "~/Images/Icons/layout_edit.png"
                },
            };
        }


        private TreeNodeMenuItem[] GetRootMenuItems(bool master)
        {
            if(master)
            {
                return new TreeNodeMenuItem[]
                {
                    new TreeNodeMenuItem() 
                    {
                        Disabled = false,
                        Id = "CreateMasterPageTemplate",
                        Title = "Create"
                    }
                };
            }
            else
            {
                return new TreeNodeMenuItem[]
                {
                    new TreeNodeMenuItem() 
                    {
                        Disabled = false,
                        Id = "CreatePageTemplate",
                        Title = "Create"
                    }
                };
            }
            
        }

        #region IContentTreePlugin Members


        public System.Web.Mvc.ActionResult InvokeAction(string action, string objectType, Guid? objectId)
        {
            switch (action)
            {
                case "CreateMasterPageTemplate":
                    return new OpenDialogResult("~/AbstractPageTemplate/Create?masterPage=true", "Create Master Page Template");
                    break;
                case "CreatePageTemplate":
                    return new OpenDialogResult("~/AbstractPageTemplate/Create?masterPage=false", "Create Page Template");
                    break;
                case "CreateContentTemplate":
                    return new OpenDialogResult("~/ContentTemplate/Create", "Create Content Template");
                    break;
                case "EditTemplate":
                    if (objectType == "PageTemplate" || objectType == "MasterPageTemplate")
                    {
                        return new TabResult(
                            "Template", UrlResolver.TemplateEditor(objectId.Value),
                            "Properties", "~/ConfigurePageTemplate/Properties/" + objectId,
                            "System", "~/ConfigurePageTemplate/BasicDetails/" + objectId);
                    }
                    break;
                case "DeleteTemplate":
                    if (objectType == "PageTemplate" || objectType == "MasterPageTemplate")
                    {
                        return this.DeletePageTemplate(objectId.Value);
                    }
                    break;
            }
            return new EmptyResult();
        }

        private ActionResult DeletePageTemplate(Guid id)
        {
            ActionResult result = new RefreshTreeNodeResult(true);

            //
            //  ok, we want to delete the page template
            SchemaService service = new SchemaService(this.CmsDataProvider);
            var pt = service.GetPageTemplateById(id);

            if (pt != null)
            {
                //
                //  ok, this is our template
                try
                {
                    service.DeletePageTemplate(pt);
                }
                catch (Exception ex)
                {
                    result = new MessageBoxResult(ex.Message);
                }
            }

            return result;
        }

        #endregion
    }
}
